package cn.edu.scnu.pay.service;

import cn.edu.scnu.pay.mapper.OrderMapper;
import cn.edu.scnu.pay.utils.SpringUtils;
import cn.edu.scnu.pojo.AlipayBean;
import cn.edu.scnu.vo.OrderStatus;
import cn.edu.scnu.vo.ResultCode;
import cn.edu.scnu.vo.SysResult;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.response.AlipayTradeRefundResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.util.UUID;

@Service
public class AlipayService {

    @Autowired
    private AlipayClient alipayClient;

    @Autowired
    private OrderMapper orderMapper;


    //    public SysResult pay(AlipayBean alipayBean) {
//        AlipayTradePagePayRequest alipayRequest = SpringUtils.getApplicationContext().getBean(AlipayTradePagePayRequest.class);
//        try {
//            alipayRequest.setBizContent(JSON.toJSONString(alipayBean));
//            String result = alipayClient.pageExecute(alipayRequest).getBody();
//            orderMapper.addOrder(alipayBean);
//            return SysResult.build(ResultCode.SUCCESS, "ok", result);
//        } catch (AlipayApiException e) {
//            e.printStackTrace();
//        }
//        return SysResult.build(ResultCode.FAIL, "error", null);
//    }
    public String pay(AlipayBean alipayBean) {
        // TODO 展示用
        alipayBean.setOut_trade_no(UUID.randomUUID().toString());

        AlipayTradePagePayRequest alipayRequest = SpringUtils.getApplicationContext().getBean(AlipayTradePagePayRequest.class);
        String result = "";
        try {
            alipayRequest.setBizContent(JSON.toJSONString(alipayBean));
            result = alipayClient.pageExecute(alipayRequest).getBody();
            orderMapper.addOrder(alipayBean);
        } catch (AlipayApiException e) {
            e.printStackTrace();
        } catch (DuplicateKeyException e) {
            return result;
        }
        return "fail";
    }

    public SysResult refund(AlipayBean alipayBean) {
        AlipayTradeRefundRequest request = SpringUtils.getApplicationContext().getBean(AlipayTradeRefundRequest.class);
        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no", alipayBean.getOut_trade_no());
        bizContent.put("refund_amount", alipayBean.getTotal_amount());
        request.setBizContent(bizContent.toString());
        AlipayTradeRefundResponse response = null;
        try {
            response = alipayClient.execute(request);
            if (response.isSuccess()) {
                orderMapper.updateOrderStatus(alipayBean.getOut_trade_no(), OrderStatus.TRADE_CLOSED.status);
                return SysResult.ok();
            } else {
                return SysResult.build(ResultCode.FAIL, response.getMsg(), null);
            }
        } catch (AlipayApiException e) {
            e.printStackTrace();
            return SysResult.build(ResultCode.INTERNAL_SERVER_ERROR, "INTERNAL_SERVER_ERROR", null);
        }
    }

    public void updateOrderStatus(String out_trade_no, OrderStatus paid) {
        orderMapper.updateOrderStatus(out_trade_no, paid.status);
    }
}
